iT邦幫忙

2022 iThome 鐵人賽

DAY 14
0
Modern Web

clojure 刷刷鍋系列 第 14

Clojure 肉片 -第 14 塊

  • 分享至 

  • xImage
  •  

【今日湯底】

Trolls are attacking your comment section!

A common way to deal with this situation is to remove all of the vowels from the trolls' comments, neutralizing the threat.

Your task is to write a function that takes a string and return a new string with all vowels removed.

For example, the string "This website is for losers LOL!" would become "Ths wbst s fr lsrs LL!".

Note: for this kata y isn't considered a vowel.

(必須通過以下測試)

(ns kata.test
  (:require [clojure.test :refer :all]
            [kata         :refer [find_shortest]]))

(deftest basic-tests
  (is (= (find_shortest "bitcoin take over the world maybe who knows perhaps") 3))
  (is (= (find_shortest "turns out random test cases are easier than writing out basic ones") 3))
  (is (= (find_shortest "lets talk about javascript the best language") 3))
  (is (= (find_shortest "i want to travel the world writing code one day") 1))
  (is (= (find_shortest "Lets all go on holiday somewhere very cold") 2))
)

【我的答案】

(ns disemvowel-trolls)

(defn disemvowel
  [string]
  (.replaceAll string "[aeiouAEIOU]" "")
)

思路:

  1. split 後找到可以 sort string length 的做法
  2. 再取出第一個值看 count

【其他人的答案】

(ns disemvowel-trolls)


(defn disemvowel
  [string]
  (clojure.string/replace string #"(?i)[aeiou]" ""))
(ns disemvowel-trolls)

(defn disemvowel [string]
  (->> string
       (remove (set "aeiouAEIOU"))
       (apply str)))

上一篇
Clojure 肉片 -第 13 塊
下一篇
Clojure 肉片 -第 15 塊
系列文
clojure 刷刷鍋30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言